package code.Frame.adminFrame;

import code.Frame.FrameManager;
import code.MYSQL;
import code.Main;
import code.SHA256;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.regex.Pattern;

public class PIFrame {
    public JPanel panel;
    private JLabel title;
    private JButton modify;
    private JButton back;
    private JList info;
    private int sel_index = -1;

    public PIFrame()
    {
        back.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                Main.frame.remove(Main.panel_now);
                Main.panel_now = FrameManager.admin_main;
                Main.frame.setContentPane(Main.panel_now);
                Main.panel_now.revalidate();
            }
        });
        modify.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                switch (sel_index)
                {
                    case 0 ->
                    {
                        String name = JOptionPane.showInputDialog("请输入修改后的姓名");
                        if (name == null)
                            return ;
                        name = "'" + name + "'";
                        if (name.length() == 2)
                        {
                            JOptionPane.showMessageDialog(Main.frame, "用户名不能为空");
                            return ;
                        }
                        if (name.length() > 32)
                        {
                            JOptionPane.showMessageDialog(Main.frame, "用户名不能超过30位");
                            return ;
                        }
                        try
                        {
                            MYSQL.maintain("update administrator set name = " + name + " where UID = " + (Main.uid - 300000000));
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                    }
                    case 1 ->
                    {
                        String phone = JOptionPane.showInputDialog("请输入修改后的电话");
                        if (phone == null)
                            return ;
                        if (phone.length() > 15 || phone.length() == 0)
                        {
                            JOptionPane.showMessageDialog(Main.frame, "不合法的电话");
                            return ;
                        }
                        if (!Pattern.compile("[0-9]*").matcher(phone).matches())
                        {
                            JOptionPane.showMessageDialog(Main.frame, "电话应为纯数字");
                            return ;
                        }
                        try
                        {
                            MYSQL.maintain("update administrator set phone = " + phone + " where UID = " + (Main.uid - 300000000));
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                    }
                    case 2 ->
                    {
                        String passwords = JOptionPane.showInputDialog("请输入修改后的密码");
                        if (passwords == null)
                            return ;
                        if (passwords.length() < 6 || passwords.length() > 20)
                        {
                            JOptionPane.showMessageDialog(Main.frame, "密码为6~20位");
                            return ;
                        }
                        String password_hash = "'" + SHA256.getSHA256StrJava(passwords) + "'";
                        try
                        {
                            MYSQL.maintain("update administrator set password = " + password_hash + " where UID = " + (Main.uid - 300000000));
                        } catch (Exception ex)
                        {
                            throw new RuntimeException(ex);
                        }
                    }
                }
                refresh_list();
            }
        });
        info.addListSelectionListener(new ListSelectionListener()
        {
            @Override
            public void valueChanged(ListSelectionEvent e)
            {
                sel_index = info.getSelectedIndex();
            }
        });
    }

    public void refresh_list()
    {
        String[] val;
        try
        {
            val = MYSQL.query_info(Main.uid);
        } catch (SQLException ex)
        {
            throw new RuntimeException(ex);
        }
        info.setListData(val);
    }
}
